/**
 * 
 */
package com.yidin.util;

import com.yidin.util.HttpEnginer.HttpEnginerNotify;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * 弹窗工作类
 * 
 * @author cola
 * @since 20120716
 */
public class ToastUtil {

	public static void showToast(Context context, String text, int showTime) {
		Toast.makeText(context, text, showTime).show();
	}

	public static void showToast(Context context, String text) {
		showToast(context, text, Toast.LENGTH_SHORT);
	}

	public static void showToast(Context context, int resId) {
		String s = context.getResources().getString(resId);
		showToast(context, s);
	}

	/**
	 * 打开下载链接
	 */
	private static void openWeb(String url, Activity activity) {
		Uri uri = Uri.parse(url);
		activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
	}

	// 显示基于Layout的AlertDialog
	public static void showDialog(final Activity context, final String url, String message) {
		activity = context;
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		builder.setTitle("温馨提示");
		builder.setMessage(message);
		builder.setPositiveButton("立即升级", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				//openWeb(url, context);
				OpenUrl(context,url );
			}
		});
		builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		builder.show();
	}
	
	private static Activity activity ;
	private static Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				activity = (Activity) msg.obj;
				Bundle b = msg.getData();
				showDialog(activity, (String)b.get("url"), "您现在使用的是营销版应用商场，访问此栏目需要升级至完整版");
				break;
			case 2:
				progressDialog.setDProgress((Integer)msg.obj);
				break;
			case 3:
				progressDialog.dismiss();
				String err = (String) msg.obj;
				Toast.makeText(activity,"更新失败：" + err, Toast.LENGTH_LONG).show();
				break;
			}
		}
	};
	//ProgressDialog
	private static  ProgressBarDialog progressDialog;
	private static void OpenUrl(final Activity context,final String url){
		//this表示该对话框是针对当前Activity的  
		progressDialog = new ProgressBarDialog(context); 
		final HttpEnginer he = new HttpEnginer(context,new HttpEnginerNotify(){
			public void progressNotify(int currentSize, int totalSize) {
				// TODO Auto-generated method stub
				double max = progressDialog.getDMax();
				max *= 1024 * 1024;
				if (max < totalSize) {
					progressDialog.setDMax(totalSize);
				} else if (currentSize >= max)
				{
					progressDialog.dismiss();
					//FileAdapter.install(FileAdapter.getSdCardRootDir() + MarketingActivity.SERVER_DATA_PATH + HttpAdapter.getNameFromUrl(url),context);
				}
				else
				{
					Message msg = new Message();
					msg.what = 2;
					msg.obj = currentSize;
					handler.sendMessage(msg);
				}
			}
			
		});
		
		if(url == null) return;
		//设置进度条风格STYLE_HORIZONTAL  
//		progressDialog.setProgressStyle(  
//		        ProgressDialog.STYLE_HORIZONTAL);  
		progressDialog.setTitle("下载进度          ");  
		int p = url.lastIndexOf("/");
		final String fileName = url.substring(p + 1, url.length());
		progressDialog.show();
		new Thread((new Runnable(){
			public void run() {
				// TODO Auto-generated method stub
				String err = "";//he.doHttpGet(url,FileAdapter.getSdCardRootDir() + MarketingActivity.SERVER_DATA_PATH,fileName);
				if(err == null || err.length() == 0) return;
				AspLog.d("err: ", err);
				Message msg = new Message();
				msg.what = 3;
				msg.obj = err;
				handler.sendMessage(msg);
			}
			
		})).start();
	}
}
